﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
	/// <summary>
	/// 单控设备场景控制编辑
	/// </summary>
	public partial class SceneDeviceItem : BaseFormControl
	{
		Device dev = null;

		public SceneDeviceItem(Device dev)
		{
			InitializeComponent();

			this.dev = dev;
			cbEnabled.Checked = false;
			txtName.Text = dev.nickName;
			selValue.bindData(dev.type.attrs[0].values);

			selValue.ValueChanged += new EventHandler(valueChange);
			cbEnabled.CheckedChanged += new EventHandler(valueChange);
		}

		// 目标设备
		public Device TagDevice
		{
			get
			{
				return dev;
			}
		}

		// 设置开
		public void open()
		{
			cbEnabled.Checked = true;
			selValue.Value = dev.type.attrs[0].values["开"];
		}

		// 设置关
		public void close()
		{
			cbEnabled.Checked = true;
			selValue.Value = dev.type.attrs[0].values["关"];
		}

		// 重写Value属性，返回SceneEvent对象
		public override object Value
		{
			get
			{
				if (cbEnabled.Checked)
				{
					SceneEvent se = new SceneEvent();
					se.device = dev.uuid;
					se.attrs.Add(dev.type.attrs[0].name);
					se.attrValueKeys.Add(selValue.Text);
					return se;
				}
				else
				{
					return null;
				}
			}
			set
			{
				if (value == null)
				{
					cbEnabled.Checked = false;
				}
				else
				{
					SceneEvent se = (SceneEvent)value;
					selValue.Text = se.attrValueKeys[0];
				}
			}
		}
	}
}
